package com.woniuxy.kaer.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.kaer.commons.util.Result;
import com.woniuxy.kaer.param.ExchangeProductParam;
import com.woniuxy.kaer.shop.mapper.PointProductMapper;
import com.woniuxy.kaer.shop.model.PointProduct;
import com.woniuxy.kaer.shop.model.PointProductOrder;
import com.woniuxy.kaer.shop.mapper.PointProductOrderMapper;
import com.woniuxy.kaer.shop.service.PointProductOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.kaer.user.UserInfoFeignClient;
import com.woniuxy.kaer.user.dto.UserInfoDTO;
import com.woniuxy.kaer.user.form.UpdatePointForm;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 郭建东
 * @since 2025年02月25日
 */
@Service
public class PointProductOrderServiceImpl extends ServiceImpl<PointProductOrderMapper, PointProductOrder> implements PointProductOrderService {

    @Resource
    private PointProductOrderMapper pointProductOrderMapper;

    @Resource
    private PointProductMapper pointProductMapper;

    @Resource
    private UserInfoFeignClient userInfoFeignClient;

    @Override
    public void exchangeProduct(ExchangeProductParam param, Integer userId) throws Exception {
        QueryWrapper<PointProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",param.getPointProductId());
        PointProduct pointProduct = pointProductMapper.selectOne(queryWrapper);
        if(pointProduct==null){
            throw new Exception("该积分商品不存在");
        }

        if(pointProduct.getPointProductStock()<param.getPointProductNum()){
            throw new Exception("该积分商品库存不足");
        }
        Result<UserInfoDTO> result = userInfoFeignClient.selectSelf(userId);
        if(result.getCode()!=200){
            throw new Exception("用户不存在");
        }
        if(result.getData().getPoint()<pointProduct.getPointNeed()*param.getPointProductNum()){
            throw new Exception("积分不足");
        }
        PointProductOrder pointProductOrder = BeanUtil.toBean(param, PointProductOrder.class);
        //通过时间戳和用户ID生成订单ID
        String pointProductOrderId = LocalDateTime.now().toString().replaceAll(":", "").replaceAll("-", "").replaceAll(" ", "").substring(0, 12);
        pointProductOrder.setPointProductOrderId("P"+userId+"-"+pointProductOrderId);

        pointProductOrder.setPointProductName(pointProductMapper.selectById(param.getPointProductId()).getPointProductName());
        pointProductOrder.setPointProductPrice(BigDecimal.valueOf(pointProduct.getPointNeed()*param.getPointProductNum()));
        pointProductOrder.setUserId(userId);
        pointProductOrder.setCreateTime(LocalDateTime.now());
        pointProductOrder.setState(0);//待发货
        pointProductOrderMapper.insert(pointProductOrder);
        //更新积分商品库存
        pointProduct.setPointProductStock(pointProduct.getPointProductStock()-param.getPointProductNum());
        pointProductMapper.updateById(pointProduct);
        UpdatePointForm updatePointForm = new UpdatePointForm();
        updatePointForm.setUserId(userId);
        updatePointForm.setPointChangeNum(-Integer.parseInt(pointProduct.getPointNeed()*param.getPointProductNum()+""));
        userInfoFeignClient.updatePoint(updatePointForm);
    }
}
